package com.wlsk.license.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wlsk.license.common.constant.CharteredLicenseConstant;
import com.wlsk.license.common.enums.CharterPlateStatusEnum;
import com.wlsk.license.common.enums.OperateLogTempEnum;
import com.wlsk.license.common.result.PlateResult;
import com.wlsk.license.common.utils.Sm4Util;
import com.wlsk.license.dao.entity.Plate;
import com.wlsk.license.dao.entity.PlateReview;
import com.wlsk.license.dto.request.CharteredReviewDetailDto;
import com.wlsk.license.dto.zz.LicenseInfoDto;
import com.wlsk.license.dto.zz.SelectStateReqDto;
import com.wlsk.license.service.ICharteredReviewService;
import com.wlsk.license.service.IPlateReviewService;
import com.wlsk.license.service.IPlateService;
import com.wlsk.license.service.zpff.CharterApplyService;
import com.wlsk.license.service.zpff.RtabTokenHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class CharteredReviewServiceImpl extends BaseService implements ICharteredReviewService {

    @Autowired
    private IPlateReviewService reviewService;
    @Autowired
    private IPlateService plateService;
    @Autowired
    private CharterApplyService applyService;

    @Override
    public PlateResult<String> createCharteredReview(String reviewBody) {
        try {
            String reqBody = Sm4Util.decryptEcb(RtabTokenHelper.KEY, reviewBody);
            CharteredReviewDetailDto detailDto = JSONObject.parseObject(reqBody, CharteredReviewDetailDto.class);
            log.info("包车牌审核结果回调:{}", detailDto);
            if(detailDto != null && StringUtils.isNotEmpty(detailDto.getAppNumber())) {
                Plate plate = plateService.getBaseMapper().selectOne(Wrappers.<Plate>lambdaQuery().eq(Plate::getTradeAppNumber, detailDto.getAppNumber()).orderByDesc(Plate::getId).last("limit 1"));
                if(plate != null) {
                    PlateReview review = new PlateReview();
                    BeanUtils.copyProperties(detailDto, review);
                    review.setPlateId(plate.getId());
                    reviewService.save(review);
                    LicenseInfoDto licenseInfo = applyService.selectState(SelectStateReqDto.builder().appnumber(plate.getTradeAppNumber()).build());
                    String state = licenseInfo.getState();
                    boolean isChanged = !plate.getTradeCharterPlateStatus().toString().equals(state);
                    if(isChanged) {
                        plate.setTradeCharterPlateStatus(Integer.parseInt(state));
                    }
                    if(StringUtils.isNotEmpty(licenseInfo.getPrintLicNo())) {
                        plate.setCharterPlateNumber(licenseInfo.getPrintLicNo());
                    }
                    if(licenseInfo.getPrintLicPlaID() != null) {
                        plate.setPrintLicPlaId(licenseInfo.getPrintLicPlaID());
                    }
                    CharterPlateStatusEnum plateStatusEnum = CharteredLicenseConstant.PLATE_TRADE_STATUS.get(Integer.parseInt(state));
                    plate.setCharterPlateStatus(plateStatusEnum.getCode());
                    plateService.updateById(plate);
                    if(isChanged) {
                        OperateLogTempEnum operateLogTempEnum = CharteredLicenseConstant.TRADE_LOG_STATUS.get(Integer.parseInt(state));
                        if(operateLogTempEnum != null) {
                            super.addOperateLog(plate.getId(), null, operateLogTempEnum, true);
                        }
                    }
                    return PlateResult.success("审核记录创建成功");
                } else {
                    return PlateResult.failed("未找到对应的包车牌信息");
                }
            } else {
                return PlateResult.failed("参数错误");
            }
        } catch (Exception e) {
            log.error("createCharteredReview处理异常-{}-{}",RtabTokenHelper.KEY, reviewBody, e);
            return PlateResult.failed("处理异常");
        }
    }
}
